<template>
    <orh-table add="add" pages @on-add="displays" :pageDate="pageDate" del="del" @on-del="confirm">
        <Table :columns="columns1" :data="SemesterList" slot="table" @on-selection-change="selectOne" :loading="tableload"  highlight-row ref="currentRowTable"></Table>
        <orh-sidebar v-model="show" :title="layouttitle" scale slot="others">
            <component :is="content" :infoId='infoId' @requery="requery"></component>
        </orh-sidebar>
    </orh-table>
</template>

<script>
export default {
    components: {
        Operation: resolve => {require(['./Operation.vue'], resolve);},
    },
    name: "basicmodule-semester",
    layout:'menu',
    data () {
        return {
            content:'',
            pageDate: {
                total: 0,
                current_page: 1,
                per_page: 1
            },
            del:[],
            show: false,
            layouttitle:null,
            delUrl:'api/semester/v1/semester/delete/',
            infoId: null,
            tableload:true,
            SemesterList:[],
            schoolyear: [],
            columns1: [
                {
                    type: 'selection',
                    width: 60,
                    align: 'center',
                },
                {
                    title: '学年名称',  
                    render: (h, params) => {
                        return h('div', params.row.schoolyear?params.row.schoolyear.schoolyear_name.zh?params.row.schoolyear.schoolyear_name.zh:params.row.schoolyear.schoolyear_name.mn:'');
                    }
                },
                {
                    title: '学期名称',          
                    render: (h, params) => {
                        return h('div', params.row.semester_name.zh?params.row.semester_name.zh:params.row.semester_name.mn);
                    }
                },
                {
                    title: '学期开始时间',
                    key: 'start_at',
                },
                {
                    title: '学期结束时间',
                    key: 'end_at'
                },
                {
                    title: '教学周开始时间',
                    key: 'teaching_start_at',
                },
                {
                    title: '教学周结束时间',
                    key: 'teaching_end_at'
                },
                {
                    title: '操作',
                    key: 'action',
                    width: 150,
                    align: 'center',
                    render: (h, params) => {
                        return h('div', [
                            h('Button', {
                                directives: [
                                    {
                                        name: 'button',
                                        value: 'edit',
                                    }
                                ],
                                props: {
                                    type: 'primary',
                                    size: 'small'
                                },
                                style: {
                                    marginRight: '5px',
                                },
                                on: {
                                    click: () => {
                                        this.update(params.row.semester_id)
                                    }
                                }
                            }, '编辑'),
                            h('Button', {
                                directives: [
                                    {
                                        name: "button",
                                        value: "del"
                                    }
                                ],
                                props: {
                                    type: 'error',
                                    size: 'small'
                                },
                                style: {
                                    marginRight: '5px',
                                },
                                on: {
                                    click: () => {
                                        this.del = [params.row.semester_id]
                                        this.confirm()
                                    }
                                }
                            }, '删除'),
                        ]);
                    }
                }
            ],
        }
    },
        
    methods: {       
        // 获取学期的数据
        Semester(index){
            this.tableload = true
            this.$http.get(`api/semester/v1/semester/lists?page=${index}`).then(val=>{
                this.SemesterList=val.data.data
                this.pageDate.total=val.data.total
                this.pageDate.per_page=val.data.per_page
                this.pageDate.current_page=val.data.current_page
                this.tableload = false  
            }).catch(err => {
                if (err.code && err.code == 422) {
                    this.$Message.error(err.message);
                }
                this.tableload = false
            })
        }, 
        // 点击编辑时向子组件传数据
        update(index){
            this.show=true
            this.content = 'Operation' 
            this.layouttitle='修改'                           
            this.infoId = index
        },   
        /************************************************************
         * 批量删除
         ************************************************************/
        selectOne(index){
            this.del = index.map(item=>item.semester_id)
        },
        /************************************************************
         * 执行删除操作
         ************************************************************/
        confirm () {
            this.$Modal.confirm({
                title: '数据删除',
                content: '<p>您确定要删除这些数据吗?</p>',
                onOk: () => {
                    if(this.del.length==0){
                        this.$Message.error('请选择删除项');
                        return
                    }
                    this.del = JSON.stringify(this.del)
                    this.tableload = true 
                    this.$http.delete(`${this.delUrl}${this.del}`).then(val=>{  
                        this.del=[]
                        this.Semester(this.pageDate.current_page)  
                        this.$Message.success(val.message);           
                    }).catch(err=>{
                        if(err.code && err.code==422) {
                            this.$Message.error(err.message);
                        }
                        this.del=[]
                        this.tableload = false 
                    })
                },
            });
        },
        // 修改返回数据更新
        requery(val){            
            if(val == true){
                this.show = false
                this.Semester(this.pageDate.current_page);
            }
        }, 
        // 添加按钮           
        displays(){
            this.infoId = null
            this.show=true
            this.content = 'Operation' 
            this.layouttitle='添加'
        },
    },
    mounted(){
        this.Semester(1);
    },
}
</script>
